/*
** Xin YUAN, 2024, BSD (2)
*/

////////////////////////////////////////////////////////////////////////////////

#include "crypt_test.h"

////////////////////////////////////////////////////////////////////////////////

uint8_t g_data1[2048];
uint8_t g_data2[16];

static void init_global_data() noexcept
{
	CTL::AdvRandomNumber2 randNum;
	randNum.Init();
	for ( int32_t i = 0; i < 2048; ++ i )
		g_data1[i] = randNum.Next() % 256;
	for ( int32_t i = 0; i < 16; ++ i ) {
		switch ( randNum.Next() % 3 ) {
		case 1:
			g_data2[i] = 'A' + (randNum.Next() % 26);
			break;
		case 2:
			g_data2[i] = 'a' + (randNum.Next() % 26);
			break;
		default:
			g_data2[i] = '0' + (randNum.Next() % 10);
			break;
		}
	}
}

int test_md5() noexcept
{
	init_global_data();

	outputf_s(_s("==========test_md5===========\n"));
	try {
		CTL::Md5Calc mc;
		mc.Initialize();
		if ( !mc.Input(g_data1, 2048) )
			CTL::ExceptionTraits::ThrowOverflow();  //throw
		if ( !mc.Input(g_data2, 16) )
			CTL::ExceptionTraits::ThrowOverflow();  //throw
		if ( !mc.End() )
			CTL::ExceptionTraits::ThrowOverflow();  //throw
		const CTL::Md5_Value& v = mc.GetValue();
		outputf_s(_s("test_md5: %08X %08X %08X %08X\n"), v.a, v.b, v.c, v.d);
	}
	catch(...) {
		outputf_s(_s("test_md5: exception!\n"));
		outputf_s(_s("***********test_md5*************\n"));
		return 1;
	}
	outputf_s(_s("***********test_md5*************\n"));

	return 0;
}

int test_crc32() noexcept
{
	init_global_data();

	outputf_s(_s("==========test_crc32===========\n"));

	CTL::CRC32Util util;
	util.BuildTable();

	CTL::CRC32Hash hash;
	hash.Initialize();
	hash.InputData(util, g_data1, 2048);
	hash.InputData(util, g_data2, 16);
	outputf_s(_s("test_crc32: %08X\n"), hash.GetValue());

	outputf_s(_s("***********test_crc32*************\n"));

	return 0;
}

////////////////////////////////////////////////////////////////////////////////
